﻿using System.Diagnostics;

namespace IdGen;

/// <summary>
/// 向 <see cref="IdGenerator"/> 提供时间数据。此时间源使用 <see cref="Stopwatch"/> 来进行计时
/// </summary>
public abstract class StopwatchTimeSource : ITimeSource
{
    private static readonly Stopwatch _sw = new();
    private static readonly DateTimeOffset _initialized = DateTimeOffset.UtcNow;

    public DateTimeOffset Epoch { get; private set; }

    protected static TimeSpan Elapsed => _sw.Elapsed;

    protected TimeSpan Offset { get; private set; }

    public StopwatchTimeSource(DateTimeOffset epoch, TimeSpan tickDuration)
    {
        Epoch = epoch;
        Offset = _initialized - Epoch;
        TickDuration = tickDuration;

        _sw.Start();
    }

    public TimeSpan TickDuration { get; private set; }

    public abstract long GetTicks();
}
